package top.leyi.handle;


import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.IApproveService;
import top.leyi.entity.Approve;
import top.leyi.entity.user.User;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;

@RestController
@RequestMapping("/api/approve")
public class ApproveHandle {
@Autowired
    IApproveService service;

    @GetMapping("/all/{page}")
    public Result getAllUndealApprove(
            @PathVariable("page") int page,
            @RequestParam("token") String token
    ) {
        return new Result(200, service.queryUndealApprove(page));
    }

    @GetMapping("/fail/{page}")
    public Result getAllFailApprove(
            @PathVariable("page") int page,
            @RequestParam("token") String token
    ) {
        return new Result(200, service.queryFailApprove(page));
    }

    @GetMapping("/suc/{page}")
    public Result getAllSucApprove(
            @PathVariable("page") int page,
            @RequestParam("token") String token
    ) {
        return new Result(200, service.querySucApprove(page));
    }


    @GetMapping("")
    public Result getOwnApprove(
            @RequestParam("token") String token,
            @RequestParam("id") int userId
         ){
        User user = new User();
        user.setId( userId);
        return  new Result(200, service.queryUserApproveStatus(user));
    }

    @PostMapping("")
    public Result addApprove(
                @RequestParam("pics") String pics,
                @RequestParam("id") int userId,
                @RequestParam("token") String token
    )throws Exception {
        Approve approve = new Approve();
        approve.setUser_id(userId);
        approve.setPics(pics);
        service.addApprove(approve);
        return ResultGenerator.OK.getResult();
    }

    @DeleteMapping("")
    public Result removeApprove(
            @RequestParam("id") int approveId,
            @RequestParam("token") String token
    ){
        Approve approve = new Approve();
        approve.setId(approveId);
        service.cancelApprove(approve);
        return ResultGenerator.OK.getResult();
    }


    @PutMapping("/suc")
    public Result ApproveSuc(
            @RequestParam("token") String token,
            @RequestParam("id") int approveId,
            @RequestParam("userId") int userId
    ) throws Exception{
        Approve approve = new Approve();
        approve.setId(approveId);
        approve.setUser_id(userId);
        service.approveSuc(approve);
        return ResultGenerator.OK.getResult();
    }

    @PutMapping("/fail")
    public Result ApproveFail(
            @RequestParam("token") String token,
            @RequestParam("id") int approveId,
            @RequestParam("msg") String msg
    ) throws Exception{
        Approve approve = new Approve();
        approve.setId(approveId);
        service.approveFail(approve, msg);
        return ResultGenerator.OK.getResult();
    }
}
